package com.gm.app.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.gm.app.model.City;
import com.gm.app.model.Country;
import com.gm.app.model.District;
import com.gm.app.model.Indication;
import com.gm.app.model.Place;
import com.gm.app.model.Region;

@Controller
public class PlaceController {

	private HashMap<Class<? extends Place>, List<? extends Place>> lists = new HashMap<Class<? extends Place>, List<? extends Place>>();

	private <T extends Place> List<T> emptyList(Class<T> clazz) {
		@SuppressWarnings("unchecked")
		List<T> list = (List<T>) lists.get(clazz);
		if (list == null) {
			list = new ArrayList<T>();
			lists.put(clazz, list);
		}
		return list;
	}

	@RequestMapping(value = "/place", method = RequestMethod.GET)
	public String getPlaces(Model m,
			@RequestParam(required = false) String description) {
		List<Place> c = emptyList(Place.class);
		if (description != null && description.length() > 0) {
			c = Place.findPlacesByDescriptionLike(description).getResultList();
		}
		m.addAttribute("place", c);
		return "place/list";
	}
	@RequestMapping(value = "/place/{id}", method = RequestMethod.GET)
	public String getPlace(@PathVariable("id") Long id, Model model) {
		Place i = Place.findPlace(id);
		model.addAttribute("place", i == null ? new Place() : i);
		return "place/view";
	}
	@RequestMapping(value = "/country/{id}", method = RequestMethod.GET)
	public String getCountry(@PathVariable("id") Long id, Model model) {
		Country i = Country.findCountry(id);
		model.addAttribute("country", i == null ? new Country() : i);
		return "place/view";
	}

	@RequestMapping(value = "/country", method = RequestMethod.GET)
	public String getCountries(Model m,
			@RequestParam(required = false) String description) {
		List<Country> c = emptyList(Country.class);
		if (description != null && description.length() > 0) {
			c = Country.findCountrysByDescriptionStartsWith(description)
					.getResultList();
		}
		m.addAttribute("country", c);
		return "place/list";
	}


	@RequestMapping(value = "/city", method = RequestMethod.GET)
	public String getCities(Model m,
			@RequestParam(required = false) String description) {
		List<City> c = emptyList(City.class);
		if (description != null && description.length() > 0) {
			c = City.findCitysByDescriptionStartsWith(description)
					.getResultList();
		}
		m.addAttribute("city", c);
		return "place/list";
	}

	@RequestMapping(value = "/city/{id}", method = RequestMethod.GET)
	public String getCity(@PathVariable("id") Long id, Model model) {
		City i = City.findCity(id);
		model.addAttribute("city", i == null ? new City() : i);
		return "place/view";
	}

	@RequestMapping(value = "/district", method = RequestMethod.GET)
	public String getDistrics(Model m,
			@RequestParam(required = false) String description) {
		List<District> c = emptyList(District.class);
		if (description != null && description.length() > 0) {
			c = District.findDistrictsByDescriptionStartsWith(description)
					.getResultList();
		}
		m.addAttribute("district", c);
		return "place/list";
	}

	@RequestMapping(value = "/district/{id}", method = RequestMethod.GET)
	public String getDistrict(@PathVariable("id") Long id, Model model) {
		District i = District.findDistrict(id);
		model.addAttribute("district", i == null ? new District() : i);
		return "place/view";
	}

	@RequestMapping(value = "/region", method = RequestMethod.GET)
	public String getRegions(Model m,
			@RequestParam(required = false) String description) {
		List<Region> c = emptyList(Region.class);
		if (description != null && description.length() > 0) {
			c = Region.findRegionsByDescriptionStartsWith(description)
					.getResultList();
		}
		m.addAttribute("region", c);
		return "place/list";
	}

	@RequestMapping(value = "/region/{id}", method = RequestMethod.GET)
	public String get(@PathVariable("id") Long id, Model model) {
		Region i = Region.findRegion(id);
		model.addAttribute("region", i == null ? new Region() : i);
		return "place/view";
	}
}
